#ifndef QLSCUSTOMPLOT_H
#define QLSCUSTOMPLOT_H

#include "qcustomplot.h"

class QLsCustomPlot : public QCustomPlot
{
	Q_OBJECT
public:
	QLsCustomPlot(QWidget *parent = nullptr);

	/* 新增图层(QCustom每个线条占据一个图层) */
	QCPGraph *AddGraph(QString graphName, QColor graphColor,
					   QCPScatterStyle::ScatterShape ssCircle = QCPScatterStyle::ssCircle);
	/* 绘制图像，自动设置绘图范围 */
	void SetData(const QVector<double> &key, const QVector<double> &value, int graphIndex = 0);

	/* 缩放标识 */
	void setZoomMode(bool zoomMode);
	/* 背景图片的尺寸 */
	QSize bgSize() const;
	void setBgSize(const QSize &bgSize);
	/* 背景图片x的范围 */
	QCPRange bgXCPRange() const;
	void setBgXCPRange(const QCPRange &bgXCPRange);
	/* 背景图片y的范围 */
	QCPRange bgYCPRange() const;
	void setBgYCPRange(const QCPRange &bgYCPRange);
	/* 背景图片 */
	QImage *bgImg() const;
	void setBgImg(QImage *bgImg);
	/* 光标划过曲线显示点的坐标 */
	void setBShowPoint(bool bShowPoint);

signals:
	/* 发送存在缩放操作 的信号 */
	void sigReportHasZoom();

protected:
	/* 鼠标双击事件 */
	void mouseDoubleClickEvent(QMouseEvent *event);
	/* 鼠标按下事件 */
	void mousePressEvent(QMouseEvent *event);
	/* 鼠标移动事件 */
	void mouseMoveEvent(QMouseEvent *event);
	/* 鼠标释放事件 */
	void mouseReleaseEvent(QMouseEvent *event);
	/* 尺寸变化事件重载 */
	void resizeEvent(QResizeEvent *event);

private:
	/* 图形初始化 */
	void PlotInit();
	/* 背景图片缩放 */
	void BgResize();
	/* 光标划过曲线显示点的坐标 */
	void ShowPoint(QMouseEvent *event);

private slots:
	/* 单击事件槽函数 */
	void slotClicked();
	/* 轴变化的槽函数 */
	void slotXRangeChanged(const QCPRange &newRange);
	void slotYRangeChanged(const QCPRange &newRange);

private:
	bool mZoomMode;									/* 缩放标识 */
	QRubberBand *mRubberBand;						/* 缩放区域 */
	QPoint mOrigin;									/* 缩放起始点 */
	bool mHasGeometry;								/* 是否为缩放 */

	QTimer *m_clickedTimer;							/* 单双击冲突定时器 */
	Qt::MouseButtons m_clickedMouseButton;			/* 单击按钮事件类型 */

	/**
	 * @brief m_bgImg
	 * @brief m_bgSize
	 * @brief m_bgXCPRange
	 * @brief m_bgYCPRange
	 * @brief m_bgShowSize
	 *
	 * 图片像素尺寸	m_bgSize = (483, 287) (px)
	 * 对应绘图范围	m_bgXCPRange(-241.5, 241.5), m_bgYCPRange(-143.5, 143.5)
	 * 实际显示像素	m_bgShowSize (px)
	 *	m_bgSize.x() / size().width() == m_bgXCPRange.size() / m_bgShowSize.width();
	 *	m_bgShowSize.width() = size().width() * m_bgXCPRange.size() / m_bgSize.x();
	 *	m_bgShowSize.height() = size().height() * m_bgYCPRange.size() / m_bgSize.y();
	 *
	 * 范围 x = (-0.5*xSize, 0.5*xSize), y = (-0.5*ySize, 0.5*ySize)
	 *
	 * 模拟：
	 * 鼠标获取范围	x1, y1, x2, y2
	 * 对应图片位置	xx1, yy1, xx2, yy2
	 *	(x1 - m_bgXCPRange.lower) / m_bgXCPRange.size() == xx1 / m_bgShowSize.width()
	 *	xx1 = (x1 - m_bgXCPRange.lower) * m_bgShowSize.width() / m_bgXCPRange.size()
	 *	yy1 = (y1 - m_bgYCPRange.lower) * m_bgShowSize.height() / m_bgYCPRange.size()
	 *	xx2 = (x2 - m_bgXCPRange.lower) * m_bgShowSize.width() / m_bgXCPRange.size()
	 *	yy2 = (y2 - m_bgYCPRange.lower) * m_bgShowSize.height() / m_bgYCPRange.size()
	 *
	 *
	 * x2 = (xAxis.range.size() * xSize / (x1.lower() + 0.5*m_bgXCPRange),
	 *						yAxis.range.size() * ),
	 *				y2 = (, )
	 * (x1.lower - (-0.5*m_bgXCPRange)) / xAxis.range().size() == xSize.x() / x2.x()
	 */
	QImage *m_bgImg;								/* 背景图片 */
	QSize m_bgSize;									/* 背景图片的尺寸 */
	QCPRange m_bgXCPRange;							/* 背景图片x的范围 */
	QCPRange m_bgYCPRange;							/* 背景图片y的范围 */
	QSize m_bgShowSize;								/* 实际显示像素 */

	bool m_bShowPoint;								/* 光标划过曲线显示点的坐标 */
};

#endif // QLSCUSTOMPLOT_H
